<?php
//requirement
include($project_path.'/install_report.php5');
$error = false;
if(!isset($_SESSION['install']['package']))
{
	echo 'updateDisplayError("«!ADMIN_INSTALL_ERROR_NOVERSIONS»");';
	$error = true;
}
else
{
	$temp_path = $project_path.'/temp/';
	$file = $_SESSION['install']['package']['name'].'.'.str_replace('.', '_', $_SESSION['install']['package']['version']).'.update.tgz';
	if(!file_exists($temp_path.$file) || filesize($temp_path.$file)!=$_SESSION['install']['package']['size'] || md5_file($temp_path.$file)!=$_SESSION['install']['package']['md5'])
	{
		echo 'updateDisplayError("«!ADMIN_INSTALL_ERROR_DOWNLOADING»");';
		$error = true;
	}
}
if(!$error)
{
	$setup = @new SimpleXMLElement($_SESSION['install']['package']['index']);
	foreach ($setup->screens[0]->requirement as $requirement)
	{
		$type = ''.$requirement['type'];
		$name = ''.$requirement['name'];
		$min_version = ''.$requirement['minversion'];
		$max_version = ''.$requirement['maxnversion'];
		switch($type)
		{
			case 'engine':
				switch($name)
				{
					case 'php':
						$support = true;
						$version = phpversion();
						break;
				}
				break;
			case 'sgdb':
				switch($name)
				{
					case 'mysql':
						$support = function_exists( 'mysql_connect' );
						ob_start();
						phpinfo();
						$php = ob_get_contents();
						ob_end_clean();
						preg_match('/Client API version[^0-9]*([0-9].[0-9].[0-9])/', $php, $match);
						$version = $match[1];//temps qu'on est pas connecté, impossible de connaitre la version de mysql...
						break;
				}
				break;
			case 'phpext':
				$support = extension_loaded($name);
				$name = 'php_'.$name.'.dll';
				$version='';
				break;
			case 'phpfunc':
				$support = function_exists(''.$requirement['function']);
				$name = $name.'()';
				$version='';
				break;
			case 'package':
				$support = true;
				$version = $conf['appli']['packages'][$_SESSION['install']['package']['name']]['version'];
				$name = $_SESSION['install']['package']['name'];
				break;
		}
		if(!$support)
		{
			echo 'updateDisplayError("«!ADMIN_INSTALL_ERROR_REQUIREMENT» '.$name.'");';
			$error = true;
			break;
		}
		if($min_version!='')
		{
			if($version=='')
			{
				echo 'updateDisplayError("«!ADMIN_INSTALL_ERROR_REQUIREMENT» '.$name.' '.$min_version.'");';
				$error = true;
				break;
			}
			elseif(fwTools::compareVersions($version, $min_version)<0)
			{
				echo 'updateDisplayError("«!ADMIN_INSTALL_ERROR_REQUIREMENT» '.$name.' '.$min_version.'");';
				$error = true;
				break;
			}
		}
		if($max_version!='')
		{
			if($version=='')
			{
				echo 'updateDisplayError("«!ADMIN_INSTALL_ERROR_REQUIREMENT» '.$name.' '.$max_version.'");';
				$error = true;
				break;
			}
			elseif(fwTools::compareVersions($version, $max_version)>0)
			{
				echo 'updateDisplayError("«!ADMIN_INSTALL_ERROR_REQUIREMENT» '.$name.' '.$max_version.'");';
				$error = true;
				break;
			}
		}
	}
}
if(!$error)
{
	$next_screen = array_shift($_SESSION['install']['screens']);
	$_SESSION['install']['screen'] = $next_screen;
	echo 'updateUpdateStepCurrent("'.$_SESSION['install']['package']['name'].'", "'.$_SESSION['install']['package']['version'].'", '.$next_screen.')
updatePerform("'.$_SESSION['install']['id'].'", "«!ADMIN_INSTALL_'.$install_pages[$next_screen].'_HEAD»", "«!ADMIN_INSTALL_'.$install_pages[$next_screen].'_STATUS»");';
	if(in_array($next_screen, array(4,5)))
	{
		echo 'updateStartProgress()';
	}
}
?>